const sequelize = require('../config/database')
const User = require('./User')
const Course = require('./Course')
const Order = require('./Order')
const Exam = require('./Exam')
const Question = require('./Question')
const UserAnswer = require('./UserAnswer')
const LearningProgress = require('./LearningProgress')

// 定义关联关系
User.hasMany(Order, { foreignKey: 'userId' })
Order.belongsTo(User, { foreignKey: 'userId' })

User.hasMany(LearningProgress, { foreignKey: 'userId' })
LearningProgress.belongsTo(User, { foreignKey: 'userId' })

Course.hasMany(LearningProgress, { foreignKey: 'courseId' })
LearningProgress.belongsTo(Course, { foreignKey: 'courseId' })

Course.hasMany(Question, { foreignKey: 'courseId' })
Question.belongsTo(Course, { foreignKey: 'courseId' })

User.hasMany(UserAnswer, { foreignKey: 'userId' })
UserAnswer.belongsTo(User, { foreignKey: 'userId' })

Question.hasMany(UserAnswer, { foreignKey: 'questionId' })
UserAnswer.belongsTo(Question, { foreignKey: 'questionId' })

Course.hasMany(Order, { foreignKey: 'courseId' })
Order.belongsTo(Course, { foreignKey: 'courseId' })

module.exports = {
  sequelize,
  User,
  Course,
  Order,
  Exam,
  Question,
  UserAnswer,
  LearningProgress
}
